package com.qn.validation.helper;

import cn.hutool.core.util.ArrayUtil;
import com.qn.validation.annotation.FieldScope;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

/**
 * 字段范围校验器
 *
 * @author QN
 * @date 2024/12/31
 */
public class FieldScopeValidator implements ConstraintValidator<FieldScope, Object> {

    private FieldScope annotation;

    @Override
    public void initialize(FieldScope constraintAnnotation) {
        this.annotation = constraintAnnotation;
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        if (value == null) {
            return false;
        }

        // 获取注解参数
        String[] in = annotation.in();
        // 校验逻辑：如果value不在in数组中，则校验不通过
        return ArrayUtil.contains(in, value);
    }

}
